

/*
 * constGridDialog.java
 *
 * Created on August 12, 2004, 2:04 PM
 */

/**
 *
 * @author  pablo
 */

package edu.umd.essic.maskbuilder;

import java.awt.*;
import javax.swing.SpinnerNumberModel;

public class MaskDepthDlg extends javax.swing.JDialog {
    public static boolean OnOK = false;
    private static int Threshold = 0;
    private int flag = 1;
    private boolean add_layer = false;
    private boolean mask_type = true;
    
    SpinnerNumberModel FlagModel = new SpinnerNumberModel(1,-10,10,1);
    
    public MaskDepthDlg(java.awt.Frame parent, boolean modal) {
        super(parent, modal);
        initComponents();
        getRootPane().setDefaultButton(okButton);
    }
    
    public int getThreshold() {
        return Threshold;
    }
    
    public int getFlag(){
        if (add_layer)
            flag = Integer.parseInt(FlagSpinner.getValue().toString());
        return flag;
    }
    
    public boolean getStatus(){
        return add_layer;
    }
    
    public boolean getMaskType(){
        return mask_type;
    }
    
    public void setThreshold(int value){
        Threshold = value;
    }
    
    public void setMaskType(int value){
        if (value==0)
            mask_type = false;
        else 
            mask_type = true;
        MaskTypeCheckBox.setSelected(mask_type);
    }
    private void initComponents() {//GEN-BEGIN:initComponents
        jPanel4 = new javax.swing.JPanel();
        MaskTypeCheckBox = new javax.swing.JCheckBox();
        jPanel1 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        ThresholdText = new javax.swing.JTextField();
        jPanel2 = new javax.swing.JPanel();
        AddButton = new javax.swing.JButton();
        FlagSpinner = new javax.swing.JSpinner();
        jPanel3 = new javax.swing.JPanel();
        okButton = new javax.swing.JButton();
        cancelButton = new javax.swing.JButton();

        getContentPane().setLayout(new java.awt.GridLayout(4, 0));

        setTitle("Reset depth threshold");
        setModal(true);
        addComponentListener(new java.awt.event.ComponentAdapter() {
            public void componentShown(java.awt.event.ComponentEvent evt) {
                formComponentShown(evt);
            }
        });
        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowClosing(java.awt.event.WindowEvent evt) {
                closeDialog(evt);
            }
        });

        jPanel4.setLayout(new java.awt.FlowLayout(java.awt.FlowLayout.LEFT));

        jPanel4.setBorder(new javax.swing.border.TitledBorder("Mask type"));
        MaskTypeCheckBox.setSelected(true);
        MaskTypeCheckBox.setText("Cell-centered mask (by default)");
        MaskTypeCheckBox.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                MaskTypeCheckBoxActionPerformed(evt);
            }
        });

        jPanel4.add(MaskTypeCheckBox);

        getContentPane().add(jPanel4);

        jPanel1.setLayout(new java.awt.FlowLayout(java.awt.FlowLayout.LEFT));

        jPanel1.setBorder(new javax.swing.border.TitledBorder("Threshold"));
        jLabel1.setText("New layer depth (meters)");
        jLabel1.setPreferredSize(new java.awt.Dimension(175, 24));
        jPanel1.add(jLabel1);

        ThresholdText.setText("0");
        ThresholdText.setPreferredSize(new java.awt.Dimension(65, 27));
        jPanel1.add(ThresholdText);

        getContentPane().add(jPanel1);

        jPanel2.setLayout(new java.awt.FlowLayout(java.awt.FlowLayout.LEFT, 14, 5));

        jPanel2.setBorder(new javax.swing.border.TitledBorder("Mask and flag"));
        AddButton.setText("Add this layer to mask");
        AddButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                AddButtonActionPerformed(evt);
            }
        });

        jPanel2.add(AddButton);

        FlagSpinner.setPreferredSize(new java.awt.Dimension(55, 27));
        FlagSpinner.addChangeListener(new javax.swing.event.ChangeListener() {
            public void stateChanged(javax.swing.event.ChangeEvent evt) {
                FlagSpinnerStateChanged(evt);
            }
        });

        jPanel2.add(FlagSpinner);

        getContentPane().add(jPanel2);

        jPanel3.setLayout(new java.awt.FlowLayout(java.awt.FlowLayout.CENTER, 10, 20));

        okButton.setText("OK");
        okButton.setPreferredSize(new java.awt.Dimension(75, 27));
        okButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                okButtonActionPerformed(evt);
            }
        });

        jPanel3.add(okButton);

        cancelButton.setText("Cancel");
        cancelButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cancelButtonActionPerformed(evt);
            }
        });

        jPanel3.add(cancelButton);

        getContentPane().add(jPanel3);

        pack();
    }//GEN-END:initComponents

    private void MaskTypeCheckBoxActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_MaskTypeCheckBoxActionPerformed
      //mask_type = MaskTypeCheckBox.isSelected();
    }//GEN-LAST:event_MaskTypeCheckBoxActionPerformed

    private void AddButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_AddButtonActionPerformed
        if (flag < 10){
            add_layer = true;
            FlagSpinner.setEnabled(true);
        } else {
            add_layer = false;
            flag = flag-1;
        }
    }//GEN-LAST:event_AddButtonActionPerformed

    private void formComponentShown(java.awt.event.ComponentEvent evt) {//GEN-FIRST:event_formComponentShown
        add_layer = false;
        ThresholdText.setText(String.valueOf(-Threshold));
        if (Threshold == 0) flag = 1;
        FlagSpinner.setEnabled(false);
        Object[] Value = new Object[] {new Integer(1),new Integer(2),new Integer(3),new Integer(4),new Integer(5),
                                       new Integer(6),new Integer(7),new Integer(8),new Integer(9),new Integer(10) };
        FlagSpinner.setValue(Value[flag]);
    }//GEN-LAST:event_formComponentShown

    private void FlagSpinnerStateChanged(javax.swing.event.ChangeEvent evt) {//GEN-FIRST:event_FlagSpinnerStateChanged
        flag = Integer.parseInt(FlagSpinner.getValue().toString());
    }//GEN-LAST:event_FlagSpinnerStateChanged
    
    private boolean CheckValues(){
        boolean answer = true;
        int val;
        try {
            val = Integer.parseInt(ThresholdText.getText());
        } catch (NumberFormatException e){ return answer = false;}
        return answer;
    }
    
    private void okButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_okButtonActionPerformed
        if (!CheckValues()){
            javax.swing.JOptionPane.showMessageDialog(this,"Wrong value!","Error",javax.swing.JOptionPane.ERROR_MESSAGE);
        } else {
            OnOK = true;
            Threshold = - Integer.parseInt(ThresholdText.getText());
            if (Threshold == 0) flag = 1;
            if (!add_layer) flag = flag-1;
            doClose(OnOK);
        }
    }//GEN-LAST:event_okButtonActionPerformed
    
    private void cancelButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cancelButtonActionPerformed
        OnOK = false;
        add_layer = false;
        flag = flag-1;
        doClose(false);
    }//GEN-LAST:event_cancelButtonActionPerformed
    
    private void closeDialog(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_closeDialog
        doClose(false);
    }//GEN-LAST:event_closeDialog
    
    private void doClose(boolean retStatus) {
        setVisible(false);
        dispose();
    }
    
       
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton AddButton;
    private javax.swing.JSpinner FlagSpinner;
    private javax.swing.JCheckBox MaskTypeCheckBox;
    private javax.swing.JTextField ThresholdText;
    private javax.swing.JButton cancelButton;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JPanel jPanel4;
    private javax.swing.JButton okButton;
    // End of variables declaration//GEN-END:variables
    
}
